package cn.mesmile.shiro.config;

import cn.hutool.core.util.StrUtil;
import cn.mesmile.shiro.module.permission.entity.Permission;
import cn.mesmile.shiro.module.role.dto.RoleVo;
import cn.mesmile.shiro.module.user.dto.UserVo;
import cn.mesmile.shiro.module.user.entity.User;
import cn.mesmile.shiro.module.user.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author zb
 * @date 2021/2/7 17:26
 * @Description:
 */
public class CustomRealm extends AuthorizingRealm {


    /**
     * 限定这个 Realm 只处理 UsernamePasswordToken
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }


    @Autowired
    private  UserService userService;

    /**
     *  用户校验 【权限】 的时候会调用该方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>> 进入到授权");
        // 获取到身份信息
        Object primaryPrincipal = principalCollection.getPrimaryPrincipal();
        User newUser = (User) primaryPrincipal;
        UserVo userVo = userService.findAllUserInfoByUsername(newUser.getUsername());
        List<RoleVo> roleList = userVo.getRoleList();
        // 从数据库中获取到[权限]
        Set<String> permissions = new HashSet<>();
        // 从数据获取【角色】
        Set<String> roles = new HashSet<>(roleList.size());

        roleList.forEach(roleVo -> {

            List<Permission> permissionList = roleVo.getPermissionList();
            String roleName = roleVo.getName();
            roles.add(roleName);

            permissionList.forEach(permission -> {
                String name = permission.getName();
                permissions.add(name);
            });

        });


        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissions);
        simpleAuthorizationInfo.setRoles(roles);

        return simpleAuthorizationInfo;
    }

    /**
     * 用户登录 的时候回调用此方法
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 进入到认证");
        // 身份信息
        Object principal = authenticationToken.getPrincipal();
        String username = (String) principal;
        // 从数据库获取密码
        User user = userService.findUserInfoByUsername(username);
//        UserVo userVo = userService.findAllUserInfoByUsername(username);
        if (user == null) {
            return null;
        }
        // 获取到密码
        String password = user.getPassword();
        if (StrUtil.isEmpty(password)){
            return null;
        }

        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user,password, this.getName());

        // 设置【盐】
        simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("aerofugia"));

        return simpleAuthenticationInfo;
    }


}
